home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 6313 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.3 KB

  1. Path: faatcrl.faa.gov!saratoga!lbona
  2. From: lbona@saratoga (lbona)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: returning ptr to struct with ptrs in it?
  5. Date: 23 Feb 1996 21:50:53 GMT
  6. Organization: FAA Technical Center, Pomona, NJ
  7. Message-ID: <4glcrtINNf4b@faatcrl.faa.gov>
  8. References: <4gk852INNbp4@faatcrl.faa.gov>
  9. Reply-To: lbona@tgf.tc.faa.gov
  10. NNTP-Posting-Host: 155.178.247.116
  11. X-Newsreader: Tin 1.1 PL4
  12.  
  13. lbona@saratoga (lbona) writes:
  14. : The following is beyond the scope of my knowledge. Perhaps someone can explain
  15. : it to me:
  16. : When I declare a function that returns a pointer to a struct that contains 
  17. : pointers, the pointer(s) at the end of the struct get set to garbage after 
  18. : being returned. Pointers not at the end are not affected.
  19.  
  20.  
  21. When I posted this question I had just finished an all-night coding
  22. session and my brain was mush. As a result my question wasn't as
  23. clear as I thought, actually it was misleading.
  24.  
  25. What I was trying to get at was this: When I declare a local variable, 
  26. bb, I know it's using temp memory off the stack. When the variable goes
  27. out of scope, i.e. leaving the function, the memory is returned to the
  28. stack. However, it is not cleared (compiler dependant of course), the 
  29. data stays the same until something else needs the memory. This is why 
  30. all the fields in the struct still have the correct values and I can 
  31. assign them to a struct, BB, of the same type that is still extant.
  32.  
  33. My real question is: why is it that if the struct ends with a pointer
  34. or pointers, that area of memory is corrupted (made non-zero in this case)
  35. yet if the struct ends in a non-pointer field the area of memory in 
  36. question is not changed? All the data is the same, including pointers in 
  37. the middle of the struct.
  38.  
  39. Anyway, as I said in my original post, I know it's better to pass the
  40. address of the struct in question to the function, and that's what I'll
  41. be doing, but this struck me as strange, and I was curious as to what
  42. was happening.
  43.  
  44.  
  45. : --------------------------------------------------------------
  46. : typedef struct aaa {
  47. :   int i;
  48. :   char name[22];
  49. :   int id;
  50. : } AAA;
  51. : typedef struct bar {
  52. :   long a;
  53. :   long b;
  54. :   int c;
  55. :   AAA *d;
  56. :   char e;
  57. :   char f[100];
  58. :   AAA *g;
  59. :   AAA *h;
  60. : } BAR;
  61. : BAR *parse_bar(char toke[]);
  62. : main()
  63. : {
  64. : BAR BB;
  65. : char toke[128];
  66. :   memset(&BB,0,sizeof(BAR));
  67. :    /* at this point BB.d == BB.g == BB.h == NULL */
  68. :     /* toke is an ascii string read in from a file */
  69. :   BB = *parse_bar(toke);
  70. :   /* at this point BB.g and BB.h are garbage, but all the other fields */
  71. :   /* in BB are correct including BB.d*/
  72. : }
  73. : BAR *parse_bar(char toke[]);
  74. : {
  75. : BAR bb;
  76. :   memset(&bb,0,sizeof(BAR));
  77. :    /* at this point BB.d == BB.g == BB.h == NULL */
  78. :   /* read data from file and parse - never touch bb.d, bb.g, or bb.h */
  79. :    /* at this point BB.d == BB.g == BB.h == NULL */
  80. :   return(&bb);
  81. : }
  82. : ----------------------------------------------------------
  83. : I know there are several work arounds, and that it's better to pass the address
  84. : of BB to parse_bar(), which is what I'll be doing. But I'm curious why this is
  85. : happening. I've tried it under K&R on Unix and ANSI on DOS and the results
  86. : were identical.
  87. : Any help would be appreciated,
  88. : thanks
  89. : --
  90. : Lou Bona
  91. : lbona@tgf.tc.faa.gov
  92.  
  93. --
  94.  
  95. Lou Bona
  96. lbona@tgf.tc.faa.gov
  97.  
  98.